// 子模块moduleA路径：store/modules/moduleA.js 
export default {
  namespaced: true,
  state: {
    text: "我是moduleA模块下state.text的值",
    carList: uni.getStorageSync('carList') || [],
    /**
     * id
     * title
     * desc 
     * image
     * price
     * count
     * state
     */
  },
  getters: {
    totalState(state) {
      return state.carList.every(v => v.state)
    },
    totalPrice(state) {
      return state.carList.filter(v => v.state).reduce((p, n) => p + n.count * n.price, 0)
    },
    totalCount(state) {
      return state.carList.reduce((p, n) => p + n.count, 0)
    }
  },
  mutations: {
    saveStorage(state) {
      uni.setStorageSync('carList', state.carList)
    },
    addCar(state, payload) {
      const item = state.carList.find(v => v.id === payload.id)
      if (item) {
        item.count += 1
      } else {
        state.carList.push(payload)
      }

      this.commit('car/saveStorage')
    },
    subCar(state, payload) {
      const item = state.carList.find(v => v.id === payload.id)
      if (item) {
        item.count -= 1
      }
      this.commit('car/saveStorage')
    },
    switchCarState(state, payload) {
      const item = state.carList.find(v => v.id === payload.id)
      if (item) {
        item.state = !item.state
      }
      this.commit('car/saveStorage')
    },
    switchCarAllState(state, payload) {
      state.carList.forEach(v => v.state = payload)
      this.commit('car/saveStorage')
    },
    deleteCarByState(state) {
      state.carList = state.carList.filter(v => !v.state)
      this.commit('car/saveStorage')
    }
  },
  actions: {

  },
}
